package com.igeek.collateral.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * TODO
 *
 * @author chh
 * @since 2024/7/15
 **/

@Slf4j
@Component
//开启定时任务
@EnableScheduling
//开启异步执行
//@EnableAsync
public class SendMail {
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private RedisTemplate redisTemplate;

    //押品的总记录数


    //出库记录与入库记录进行对比，有异常的向管理员的邮箱发送信息


    public static final String OutNum="outNum::";

    private String from = "2792545208@qq.com";

    //向管理员邮箱发送定时任务
    private String to = "3168850469@qq.com";

    private String subject = "定时发送任务";
    //@Async
    @Scheduled(cron = "0 38 22 * * ?  ")
    public void printBy5() throws MessagingException, FileNotFoundException {
        String content = "库存的总盘点数：";

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage , true);  //multipart设置为true代表允许支持携带附件
        helper.setFrom(from+"(岑欢欢)");
        helper.setTo(to);
        helper.setSubject(subject);
        //从redis中读取库存总记录
        Object o = redisTemplate.opsForValue().get(OutNum);
        String s = String.valueOf(o);
        helper.setText(content+s);
        javaMailSender.send(mimeMessage);
        log.info("携带信息进行邮件发送完毕~");
    }

}
